<?php
                                                                                    
if(!defined('IN_ADAGPS'))
{
    die(SYSTEM_ACCESS_DENIED);
}
$objXajax->registerFunction('fmfrmfilm_loadGrid');      
$objXajax->registerFunction('fmfrmfilm_save');
$objXajax->registerFunction('fmfrmfilm_edit');
$objXajax->registerFunction('fmfrmfilm_delete');
$objXajax->registerFunction('fmfrmfilm_init');

function fmfrmfilm_init()
{
    global $objDbSelect,$user_id,$tbl_postfix,$lang,$path;
    $objResponse =  new xajaxResponse();
    $arrCategory = fmfrmfilm_category();
    $oTbs =  new clsTinyButStrong();
    $oTbs->LoadTemplate('templates/'.$lang.'/'.$path.'/fm_film_category.html');
    $oTbs->MergeBlock('blk_data', $arrCategory);
    
    $arrFilmtype = fmfrmfilm_filmtype();
    $oTbs1 =  new clsTinyButStrong();
    $oTbs1->LoadTemplate('templates/'.$lang.'/'.$path.'/fm_film_type.html');
    $oTbs1->MergeBlock('blk_data1', $arrFilmtype);
    $oTbs1->Show(TBS_NOTHING);
    $objResponse->addAssign('fmfrmfilm_tdcategory', 'innerHTML', $oTbs->Source);
    $objResponse->addAssign('fmfrmfilm_tdfilmtype', 'innerHTML', $oTbs1->Source);
    $objResponse->addScript('changeWaiting(1);');
    //$objResponse->addScript('xajax_fmfrmfilm_loadGrid();');     
    return $objResponse->getXML();
}

function fmfrmfilm_category(){
  global $objDbSelect,$lang;
  $sql = "select id,name_vi as name from fm_category";
  $arr = $objDbSelect->GetArray($sql);
  return $arr;   
}

function fmfrmfilm_filmtype(){
  global $objDbSelect,$lang;
  $sql = "select id,name as name from fm_filmtype";
  $arr = $objDbSelect->GetArray($sql);
  return $arr;   
}

function fmfrmfilm_save($params)
{
    global $objDbSelect,$user_id,$tbl_postfix,$hotel_id;
    $objResponse =  new xajaxResponse();
    $name_vi = $params['txtnamemenuVi'];
    //$name_en = $params['txtnamemenuEn'];
    $category = $params['rdcategory'];
    $filmtype = $params['rdfilmtype'];  
      
    $timeLong = $params['txttimeLong'];    
    $quanlity = $params['txtquanlity'];
        
    $edit = $params['hdEdit'];
    $id = $params['hdId'];
    $sql = ""; 
    if($edit==1){
        $sql ="UPDATE fm_film SET name_vi = '$name_vi',time_long = '$timeLong',quanlity = '$quanlity',category = '$category',
            filmtype = '$filmtype' WHERE id =$id"; 
        $objDbSelect->Execute($sql);
        
    }else{
          $sql= " INSERT INTO fm_film(name_vi,time_long,quanlity,category,filmtype,service_id) VALUES('$name_vi','$timeLong','$quanlity',$category,$filmtype,$hotel_id)"; 
          $objDbSelect->Execute($sql); 
     }    
    $objResponse->addScript('changeWaiting(1);');
    $objResponse->addScript('xajax_fmfrmfilm_loadGrid();');     
    return $objResponse->getXML();
}


function fmfrmfilm_loadGrid()
{ 
    global $lang,$path;
    $objResponse =  new xajaxResponse();// dong luon luon co khi goi ham ajax
    $arrGrid = fmfrmfilm_getGrid(); 
    //echo '<pre>'; print_r($arrGrid); die();   
    $oTbs =  new clsTinyButStrong();
    $oTbs->LoadTemplate('templates/'.$lang.'/'.$path.'/fm_film_grid.html');
    $oTbs->MergeBlock('blk_data', $arrGrid);
    $oTbs->Show(TBS_NOTHING);
    $objResponse->addAssign('fmfrmfilm_divGrid', 'innerHTML', $oTbs->Source);
    $objResponse->addScriptCall("dataTableObj.CreatDataTableGroup('fmfrmfilm_tbl');");     
    $objResponse->addScript('changeWaiting(1);');                        
    return $objResponse->getXML(); // dong luon luon co khi goi ham ajax   
}
function fmfrmfilm_getGrid()
{
    global $objDbSelect,$lang,$hotel_id;
    $sqlSelect = " select fm.time_long,fm.quanlity,fm.id,fm.active, fm.name_vi,fm.name_en ,c.name_vi as category,ft.name  as filmtype
                   from fm_film as fm INNER JOIN fm_category  as c ON 
                   fm.category = c.id JOIN fm_filmtype as ft ON ft.id = fm.filmtype  WHERE  fm.service_id =$hotel_id 
                    order by c.id,fm.name_vi ";  
                                   
    return $objDbSelect->GetArray($sqlSelect);
}  

    function fmfrmfilm_edit($id){
        global $objDbSelect,$tbl_postfix;
        $objResponse =  new xajaxResponse();
        $sql = " SELECT * FROM fm_film WHERE id = '$id' ";
        $arr = $objDbSelect->GetArray($sql);
        $name_vi        = $arr[0]['name_vi'];
        $name_en   = $arr[0]['name_en'];    
        $category     = $arr[0]['category'];
        $filmtype         = $arr[0]['filmtype'];
               
        $time_long         = $arr[0]['time_long'];       
        $quanlity         = $arr[0]['quanlity'];       
        
        $objResponse->addScript("$('#fmfrmfilm_txtnamemenuVi').val('$name_vi');");
        $objResponse->addScript("$('#fmfrmfilm_txttimeLong').val('$time_long');");
        $objResponse->addScript("$('#fmfrmfilm_txtquanlity').val('$quanlity');");
        $objResponse->addScript("$('#fmfrmfilm_category$category').attr('checked','checked');"); 
        $objResponse->addScript("$('#fmfrmfilm_filmtype$filmtype').attr('checked','checked');"); 
        
        $objResponse->addScript('changeWaiting(1);');
        return $objResponse->getXML();   
    }
    
?>
